//引入axios
import axios from "axios";

//引入qs
import qs from "qs";

//引入路由跳转


//设置axios 默认配置
const http = axios.create({
    baseURL: "/api"
});

//设置请求拦截器
http.interceptors.request.use(req => {
    req.headers.authorization = sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')).token : null
    return req;
});

//设置请求拦截器
http.interceptors.response.use(res => {
    //全局拦截  是否登录过期 获取缺少参数
    return res.data;
});

//设置get请求
export const get = (url, params = {}) => {
    return http.get(url, { params })
};

//设置post请求
export const post = (url, params = {}, isFile = false) => {
    //参数序列化
    let data;
    //判断是否有文件上传
    if (isFile) {
        for (let k in params) {
            data.append(k, params[k]);
        };
    } else {
        data = qs.stringify(params);
    };


    return http.post(url, data);
}